﻿package de.gameduell.framework.facebook.app {
	import fl.transitions.TweenEvent;
	import fl.transitions.easing.Regular;

	import de.gameduell.framework.asset.AssetEvent;
	import de.gameduell.framework.debug.Debug;
	import de.gameduell.framework.facebook.FBBaseAppLayout;
	import de.gameduell.framework.facebook.FBBaseController;
	import de.gameduell.framework.facebook.FBBasePageManager;
	import de.gameduell.framework.facebook.FeatureList;
	import de.gameduell.framework.facebook.IPage;
	import de.gameduell.framework.facebook.app.pages.BuddyList;
	import de.gameduell.framework.facebook.app.pages.ForeignPyramidPage;
	import de.gameduell.framework.facebook.app.pages.JackpotPage;
	import de.gameduell.framework.facebook.app.pages.StartPage;
	import de.gameduell.framework.facebook.app.pages.StatusPage;
	import de.gameduell.framework.facebook.pages.Footer;
	import de.gameduell.framework.facebook.pages.TabBar;
	import de.gameduell.framework.gui.animation.GDTween;
	import de.gameduell.framework.util.FlashVars;

	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;

	public class PageManager extends FBBasePageManager {
		public static const START_PAGE:String = "start";
		public static const STATUS_PAGE:String = "status";
		public static const FOOTER:String = "footer";
		public static const TABS:String = "tabs";
		public static const BUDDY_LIST:String = "buddylist";
		public static const FOREIGN_PYRAMID:String = "foreign_pyramid";
		public static const JACKPOT:String = "jackpot";
		public static const BRICKS:String = "bricks";
		private static var instance:PageManager;
		private var layout:FBBaseAppLayout;
		private var controller:Controller;
		private var placeholders:Array = [0];

		public static function getInstance():PageManager {
			if(instance == null) {
				instance = new PageManager();
			}
			return instance;
		}

		public function PageManager() {
			super(Controller.getInstance());
			layout = FBBaseAppLayout.getInstance();
			coverUp.alpha = 0;
			coverUp.y = layout.contentPosition.y;
			
			controller = Controller.getInstance();
			controller.addEventListener(FBBaseController.JACKPOT_GAME_END, onGameFinished);
			baseController.addEventListener(FBBaseController.GAME_END, onGameFinished);
			baseController.addEventListener(FBBaseController.TUTORIAL_FINISHED, onTutorialFinished);
			controller.featureList.addEventListener(FeatureList.FEATURES_LOADED, featuresLoaded);
			controller.addEventListener(AssetEvent.COMPLETE, bricksLoaded);
			controller.loadAsset(BRICKS);
		}

		override public function show(pageId:String):void {
			if(pages[pageId] != null) {
				var aPage:IPage = IPage(pages[pageId]);
				if(_currentPage != null) {
					if(_currentPage.isContent && IPage(pages[pageId]).isContent && !isSameContent(pageId)) {
						Debug.trace("PageManager::override public function show: HIDE CURRENT PAGE", Debug.DEFAULT);
						_currentPage.hide();
					}
				}
				if(!isSameContent(pageId)) {
					_currentPage = aPage.isContent ? aPage : _currentPage;
					timeline.addChild(aPage.sprite);
					Debug.trace("PageManager::override public function show: IS NOT SAME CONTENT: " + _currentPage, Debug.DEFAULT);
				}
				aPage.show();	
				if(aPage.isContent) {
					tracker.appContext = pageId;
				}
			}
		}

		override public function init(aTimeline:Sprite):void {
			super.init(aTimeline);
			addPage(new StartPage(this, START_PAGE));
			addPage(new StatusPage(this, STATUS_PAGE));
			addPage(new Footer(this, FOOTER));
			addPage(new TabBar(this, TABS));
			addPage(new BuddyList(this, BUDDY_LIST));
			addPage(new ForeignPyramidPage(this, FOREIGN_PYRAMID));
		}

		public function prepareTutorialStart(event:Event = null):void {
			covered = true;
			var coverTween:GDTween = new GDTween(coverUp, "alpha", Regular.easeIn, coverUp.alpha, 1, 1, false);
			coverTween.addEventListener(TweenEvent.MOTION_FINISH, startTutorial);
			coverTween.start();
		}

		public function prepareGameStart(event:Event = null):void {
			if(controller.gameData.lives > 0) {
				covered = true;
				var coverTween:GDTween = new GDTween(coverUp, "alpha", Regular.easeIn, coverUp.alpha, 1, 1, false);
				coverTween.start();
			}
		}

		public function startTutorial(event:Event = null):void {
			Debug.trace("PageManager::startTutorial: " + layout.contentPosition.y);
			controller.startTutorial(controller.tutorialType, 760, 558, FlashVars.getKeyValue(FlashVars.LANGUAGE), layout.contentPosition.x, layout.contentPosition.y);
		}

		public function onTutorialFinished(event:Event):void {
			covered = false;
			coverUp.alpha = 0;
		}

		public function onGameFinished(event:Event):void {
			covered = false;
			coverUp.alpha = 0;
		}

		public function getPicFromLib(bt:String):MovieClip {
			var m:MovieClip = new MovieClip;
			try {
				m.addChild(library.createClip(bt));
			}catch(error:Error) {
				var ph:Placeholder = new Placeholder(bt);
				placeholders.push(ph);
				m.addChild(ph.getClip()); // new Bitmap(library.createBitmapData("brick_select_1.png", 90, 60));
			}
			return m;
		}

		//		public function loadBricks():void {
		//			Debug.trace("PageManager::loadBricks", Debug.DEBUG);
		//			var swfasset:SWFAsset = new SWFAsset("award_icons", "media/flash/drg_app/bricks.swf");
		//			swfasset.addEventListener(AssetEvent.COMPLETE, bricksLoaded);
		//			//	Testmode
		//			//	new DelayAction(5000, true);
		//			//	new AssetAction(swfasset, true);
		//			swfasset.load();
		//		}
		private function bricksLoaded(event:AssetEvent):void {
			Debug.trace(event.asset.id, Debug.DEBUG);
			if(event.asset.id == BRICKS) {
				Debug.trace("PageManager::bricksLoaded", Debug.DEBUG);
				// library for bricks should be available now
				for (var i:int = 0;i < placeholders.length;i++) {
					try {
						Placeholder(placeholders[i]).reload();
					}catch(error:Error) {
					}
				}
			}
		}

		private function featuresLoaded(event:Event = null):void {
			Debug.trace("PageManager::featuresLoaded", Debug.DEBUG);
			Debug.trace("PageManager::init jp enabled: " + controller.featureList.isEnabled(Controller.JACKPOT_FEATURE_ID));
			if(controller.featureList.isEnabled(Controller.JACKPOT_FEATURE_ID)) {
				addPage(new JackpotPage(this, JACKPOT));
			}
		}
	}
}
